#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=15;
int read(){
    int f=1;int m=0;char c=getchar();
    while((c<'0'||c>'9')){if(c=='-')f=-1;c=getchar();}
    while(!(c<'0'||c>'9')){m=m*10+(c-'0');c=getchar();}
    return m*f;
}
int n,m;
ll a[maxn];
ll b[maxn];
ll c[maxn];
int k;
string s;
bool suf[40][maxn];
int x[5];
bool win[10];
ll ans;
vector<int>vec;
vector<int>tec;
void check(){
    // cout<<(1<<k)<<'\n';
    vec.clear();tec.clear();
    for(int i=1;i<=(1<<k);i++){
        vec.push_back(b[i]);
    }int kk=1;
    while(kk<=k){
        tec.clear();
        for(int i=1;i<=vec.size();i++){
            if(suf[kk][(i+1)/2]){
                if(vec[i]>=kk)tec.push_back(i+1);
                else tec.push_back(i);
            }else {
                if(vec[i-1]>=kk)tec.push_back(i);
                else tec.push_back(i+1);
            }
        }kk++;
        vec.clear();
        for(int i=0;i<tec.size();i++)vec.push_back(b[tec[i]]);
    }
    win[tec[0]]=1;
}
void solve2(int p){
    memset(win,0,sizeof win);
    for(int i=1;i<=c[p];i++)b[i]=a[i]^x[(i%4)];
    for(int i1=0;i1<=3;i1++)for(int i2=0;i2<=3;i2++)for(int i3=0;i3<=3;i3++)for(int i4=0;i4<=3;i4++)
    for(int i5=0;i5<=3;i5++)for(int i6=0;i6<=3;i6++)for(int i7=0;i7<=3;i7++)for(int i8=0;i8<=3;i8++){
        if(c[p]<8)b[8]=i8;
        if(c[p]<7)b[7]=i7;
        if(c[p]<6)b[6]=i6;
        if(c[p]<5)b[5]=i5;
        if(c[p]<4)b[4]=i4;
        if(c[p]<3)b[3]=i3;
        if(c[p]<2)b[2]=i2;
        if(c[p]<1)b[1]=i1;
        check();
    }ll sum=0;
    for(int i=1;i<=(1<<k);i++){
        if(win[i])sum+=i;
    }ans=ans^(p+sum);
    
}
void solve(){
    ans=0;
    for(int i=0;i<4;i++)x[i]=read();
    for(int i=1;i<=n;i++)b[i]=a[i]^x[(i%4)];
    for(int i=1;i<m;i++){
        solve2(i);
    }
    cout<<ans<<'\n';
}
int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    n=read();m=read();
    if(n>8||m>8)return 0;
    for(int i=1;i<=n;i++)a[i]=read();
    for(int i=1;i<=m;i++)c[i]=read();
    k=1;while((1<<k)<n)k++;
    for(int l=1;l<=k;l++){
        cin>>s;
        for(int i=0;i<s.size();i++){
            if(s[i]=='0')suf[l][i+1]=0;
            else suf[l][i+1]=1;
        }
    }
    int T;
    T=read();
    while(T--){
        solve();
    }
    return 0;
}